package com.domor.xxx.system.controller.demo.controller;

import com.domor.xxx.system.service.WebSocketServer;
import com.domor.xxx.system.domain.SysMsg;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;

/**
 * <h3>WebSocketController DEMO</h3>
 * @author liyuyang
 * @date 2020/5/21
 */
@RequestMapping("/ws")
@RestController
public class WebSocketController {

    @GetMapping("/index")
    public ResponseEntity<String> index(){
        return ResponseEntity.ok("请求成功");
    }

    @GetMapping("/page")
    public ModelAndView page(){
        return new ModelAndView("/demo/websocket");
    }

    @RequestMapping("/push/{toUserId}")
    public ResponseEntity<String> pushToWeb(String message, @PathVariable String toUserId) throws IOException {
        SysMsg msg = new SysMsg();
        msg.setContent(message);
        msg.setReceiver(toUserId);
        WebSocketServer.send(msg);
        return ResponseEntity.ok("MSG SEND SUCCESS");
    }

}

